package com.heima.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class AdminLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String adminName = request.getParameter("adminName");
        String adminPassword1 = request.getParameter("adminPassword1");
        String adminPassword2 = request.getParameter("adminPassword2");
        PrintWriter pw = null;

        response.setContentType("text/html;charset=utf-8");
        pw = response.getWriter();

        if("admin".equals(adminName)&&"admin".equals(adminPassword1)&&"admin".equals(adminPassword2)){
            //管理员登录成功
            pw.print("<center><font style='color:green;font-size:40'>管理员登录成功</font></br>");
            pw.print("<h1><a href='/web02/find'>开始使用</a></h1></center>");
            HttpSession session = request.getSession();
            session.setAttribute("admin_ok","1");//做个记录，防止跳过管理员登录，直接访问管理员操作页面
        }
        else{
            //管理员登录失败
            pw.print("<center><font style='color:red;font-size:40'>管理员登录失败，重新登录？</font>");
            pw.print("<a href='/web02/admin_login.html'>重新登录</a></center>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
